/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action;

import java.util.ArrayList;
import java.util.List;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Administrator;
import com.gxbadge.util.MD5;
import com.opensymphony.xwork2.ActionContext;

public class AdminAction extends BaseAction {
	
	private Administrator admin;
	private List<Administrator> adminList;
	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}
	public List<Administrator> getAdminList() {
		return adminList;
	}

	public void setAdminList(List<Administrator> adminList) {
		this.adminList = adminList;
	}

	private int totalCount;
	private int pageNo;
	private int pageCount;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String find_admin() throws Exception{
		
		ActionContext ctx = ActionContext.getContext();
		
		if(this.getAdmin()!=null&&this.getAdmin().getAdminId()!=null){
			
			Administrator admin = adminManager.findAdminByAdminId(this.getAdmin().getAdminId());
			if(admin!=null){
				
				this.setAdmin(admin);
				
			}
		}else if(ctx.getSession().get("admin")!=null){
			
			Administrator admin = (Administrator)ctx.getSession().get("admin");
			
			if(admin.getAdminId()!=null){
				
				Administrator a = adminManager.findAdminByAdminId(admin.getAdminId());	
				if(a!=null){
					
					this.setAdmin(a);
					ctx.getSession().put("admin", a);
				}
			}
			
		}
		
		return "admin_find_success";
	}
	
	public String modify_admin() throws Exception {
		
		
		if(this.getAdmin().getPassword()!=null){

			String npassword =this.getAdmin().getPassword();
			String opassword = adminManager.findAdminByAdminId(this.getAdmin().getAdminId()).getPassword();
			if(!npassword.equals(opassword)){
				npassword = MD5.encoderByMd5(npassword);
				this.getAdmin().setPassword(npassword);
			}

		}
		
		adminManager.modify_adminByAdmin(this.getAdmin());
		this.setAdmin(null);
		return "admin_modify_success";
	}
	
	private List<Administrator> adList;
	public List<Administrator> getAdList() {
		return adList;
	}

	public void setAdList(List<Administrator> adList) {
		this.adList = adList;
	}
	private String paramPageNo;
	
	public String getParamPageNo() {
		return paramPageNo;
	}

	public void setParamPageNo(String paramPageNo) {
		this.paramPageNo = paramPageNo;
	}
	public String find_adminList() throws Exception {
		
		if(this.getParamPageNo()!=null){
			
			int pageNo = Integer.parseInt(this.getParamPageNo());
			this.setPageNo(pageNo);
			this.setParamPageNo(null);
		}
		
		List<Administrator> aList = adminManager.findAdminListAll();
		
		if(aList!=null){
			
			this.setAdList(aList);
			this.setTotalCount(this.getAdList().size());
			this.setPageCount(this.getTotalCount()/pageSize+1);
		}
		if(this.getPageNo()==0){
			this.setPageNo(1);
		}
		if(this.getPageCount()==0){
			this.setPageNo(0);
		}
		if(this.getPageNo()==1){
			List<Administrator> al = new ArrayList<Administrator>();
			if(this.getAdList()!=null){
				for(int i=0;i<pageSize&&i<this.getAdList().size();i++){
					Administrator a = this.getAdList().get(i);
					if(a!=null){
						al.add(a);
					}
				}
				this.setAdminList(al);
				
				if(al!=null){
					al=null;
				}
			}
			
		}
		if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
			
			List<Administrator> al = new ArrayList<Administrator>();
			
			int firstResult = (this.getPageNo()-1)*pageSize;
			if(this.getAdList()!=null&&this.getAdList().size()>=1){
				for(int i=firstResult;i<firstResult+pageSize&&i<this.getAdList().size();i++){
					
					Administrator a = this.getAdList().get(i);
					if(a!=null){
						al.add(a);
					}
				}
				this.setAdminList(al);
				if(al!=null){
					al=null;
				}
			}
		}
		
		return "adminList_find_success";
	}
	

	public String check_admin() throws Exception {
		
		
		if(this.getAdmin().getAdminId()!=null){
			
			Administrator a = adminManager.findAdminByAdminId(this.getAdmin().getAdminId());
			if(a!=null){
				
				if(a.getPermState()){
					a.setPermState(false);
				}else{
					a.setPermState(true);
				}
				adminManager.modify_adminByAdmin(a);
			}
			
		}
		

		return "admin_check_success";
	}
	
	public String del_admin() throws Exception{
		
		if(this.getAdmin().getAdminId()!=null){
			
			Administrator admin = adminManager.findAdminByAdminId(this.getAdmin().getAdminId());
			if(admin!=null){
				
				adminManager.del_admin(admin);
			}

		}
		this.setAdmin(null);
		return "admin_del_success";
	}
	
	public String insert_admin() throws Exception{
		
		if(this.getAdmin()!=null){
			this.setAdmin(null);
		}
		return "admin_insert_success";
	}
	
	public String add_admin() throws Exception{
		
		if(this.getAdmin()!=null){
			
			if(this.getAdmin().getPassword()!=null){
				String password = MD5.encoderByMd5(this.getAdmin().getPassword());
				this.getAdmin().setPassword(password);
			}
			this.getAdmin().setPermState(false);
			adminManager.add_admin(admin);
			
		}
		this.setAdmin(null);
		return "admin_add_success";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
